package com.net.xpay.common.manager.impl.stat;

import com.google.common.base.Preconditions;
import com.net.xpay.common.manager.stat.UserTransStatManager;
import com.net.common.annotation.ReadTransactional;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.common.manager.AbstractManager;
import com.net.xpay.common.dao.stat.UserTransStatMapper;
import com.net.xpay.common.domain.stat.UserTransStat;
import com.net.xpay.common.model.UserTransStatMonthModel;
import com.net.xpay.common.model.UserTransStatSumModel;
import com.net.xpay.core.model.UserTransStatSearchModel;
import com.net.xpay.common.model.UserUnionTransStatResultModel;
import com.net.xpay.common.model.search.UserUnionTransStatSearchModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;


@Component
@ReadTransactional
public class UserTransStatManagerImpl extends AbstractManager<UserTransStat> implements UserTransStatManager {

    @Autowired
    private UserTransStatMapper mapper;


    @Override
    protected BasicMapper<Long, UserTransStat> getMapper() {
        return mapper;
    }


    @Override
    public UserTransStat findByDayAndUserId(LocalDate day, Long userId) {
        UserTransStat condition = new UserTransStat();
        condition.setDay(day);
        condition.setUserId(userId);
        return getByCondition(condition);
    }

    @Override
    public List<UserTransStat> search(UserTransStatSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        page.addOrder("id", "desc");
        return nonEmptyList(mapper.search(searchModel, page));
    }

    @Override
    public long searchCount(UserTransStatSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.searchCount(searchModel);
    }

    @Override
    public List<UserTransStatMonthModel> sumMonthUserTransStat(int year, int month, Long userId) {
        Preconditions.checkArgument(userId != null);
        List<UserTransStatMonthModel> userTransStatMonthModels = mapper.sumMonthUserTransStat(year, month, userId);
        if (userTransStatMonthModels == null) {
            return new ArrayList<>();
        }
        return userTransStatMonthModels;
    }

    @Override
    public List<UserTransStat> list(UserTransStatSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return nonEmptyList(mapper.list(searchModel));
    }

    @Override
    public UserTransStatSumModel sumUserSafePayTransStat(UserTransStatSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.sumUserSafePayTransStat(searchModel);
    }

    @Override
    public List<UserUnionTransStatResultModel> searchUnion(UserUnionTransStatSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        page.addOrder("id", "desc");
        page.addOrder("create_time", "desc");
        return mapper.searchUnion(searchModel, page) == null ? new ArrayList<>() : mapper.searchUnion(searchModel, page) ;
    }

    @Override
    public long searchUnionCount(UserUnionTransStatSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.searchUnionCount(searchModel);
    }
}
